
<style>
    .spanColor{
        color: red;
    }
    .layui-form-label {
        width: 150px;
    }
    .layui-input{
        width: 200px;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">报失单号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入报失单号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input" type="text"
                                               placeholder="选择时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startTime" id="startTime" type="hidden"/>
                                    <input name="endTime" id="endTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">部门：</label>
                                    <div class="layui-input-inline">
                                        <select name="departmentId" lay-filter="departmentId" lay-search="" class="common-select-search"></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">操作人：</label>
                                    <div class="layui-input-inline">
                                        <select name="creatorId" lay-search="" class="common-select-search"></select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="lossExport" class="layui-btn icon-btn color-green">导出
                            </button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="useInventoryLossTable" lay-filter="useInventoryLossTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="useInventoryLossTableBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">设置</a> -->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        admin.selectSearch();

        // 渲染表格
        var insTb = table.render({
            elem: '#useInventoryLossTable',
            url: config.base + 'enterprisecab/cabinet/admin/useInventoryLoss/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            limit: 5,
            limits:[5,10,20,30,40,50,60,70,80],
            cellMinWidth: 100,
            cols: [[
                // {type: 'checkbox'}
                {type: 'numbers', title: '序号', fixed: 'left',}
                ,{field: 'skuPic', title: '图片', fixed: 'left', width: 110,templet:function (d) {
                        if(d.skuPic){
                            return '<img src="'+ d.skuPic +'"/>';
                        }
                        return '<img src="assets/images/default-icon.png"/>';
                    }}
                ,{field: 'skuName', title: '物料名称', fixed: 'left', width: 160}
                ,{field: 'skuCode', sort: true, title: '物料编号', fixed: 'left', width: 160}
                ,{field: 'skuSpecOld', title: '规格码', fixed: 'left', width: 160}
                ,{field: 'productBrandName', title: '品牌'}
                ,{field: 'code', title: '报失单号', width: 160}
                , {field: 'departmentName', sort: false, title: '部门'}
                , {field: 'costCenter', sort: false, title: '成本中心'}
                ,{field: 'packName', title: '包装规格'}
                ,{field: 'quantity', title: '数量(包装计)',templet:function (d) {
                        return d.quantity + d.basicsUnitName;
                    }, width: 110}
                ,{field: 'unitName', title: '包装单位'}
                ,{field: 'quantityPack', sort: true, title: '数量(基础单位计)', width: 150}
                ,{field: 'basicsUnitName', sort: true, title: '基础单位'}
                ,{field: 'marketPrice', title: '市场价'}
                ,{field: 'typeDesc', title: '类型'}
                ,{field: 'creatorName', sort: true, title: '报失人', width: 110}
                ,{field: 'createTime', sort: true, title: '报失时间', width: 160}
                // ,{align: 'center', toolbar: '#useInventoryLossTableBar', title: '操作', width: 200}
            ]],
        });

        // 工具条点击事件
        table.on('tool(useInventoryLossTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                // showUpdataModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        // 工具条点击事件
        table.on('tool(addTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'add') { //添加智能柜存放物资类型
                layer.confirm('确定要添加到智能柜存放物资类型？', {title: "操作提示", btn: ['确定', '取消']}
                , function(){ // 确定操作
                        addSearch(data);
                });
            }
        });

        //监听排序
        table.on('sort(useInventoryLossTable)', function(obj){
            table.reload('useInventoryLossTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#storeProductBtnAdd').click(function () {
            showEditModel();
        });

        function addSearch(data){
            admin.req('enterprisecab/cabProduct/admin/storeProductType/upProductType',data, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    form.render('select');
                    table.reload('addTable');
                    table.reload('useInventoryLossTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'PUT');
        }

        /**
         * 加载地址HTML
         * @param data 下拉菜单数据
         * @param tips select 默认提示
         * @param htmltarget html要插入的位置
         * @param name  下拉框需要显示值的属性
         */
        function loadSelectHtml(data, tips, htmltarget, name) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                html += `<option value='${data[i].id}'>${data[i][name]}</option>`;
            }
            $("[name='" + htmltarget + "']").html(html);
            form.render('select');
        }


        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/system/admin/dic/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }
        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });
        // 重置搜索
        form.on('submit(resets)', data => {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
            loadReportSelectHtml([],'请选操作人','creatorId',2,'.lay-header');
        });
        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    let dateValue = value.split(" - ");
                    $("#startTime").val(dateValue[0] + " 00:00:00");
                    $("#endTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }
                setTimeout(function() {$('[lay-filter="formSubSearchLog"]').click()}, 300);
            }
        });

        // 导出
        $('#lossExport').click(function () {
            var sTime = $('#startTime').val();
            var eTime = $('#endTime').val();

            if (sTime == "" && eTime == "") {
                layer.msg('请先选择创建时间！')
            } else {

                location.href = config.base + 'enterprisecab/cabinet/admin/useInventoryLoss/export?startCreateTime=' + sTime + '&endCreateTime=' + eTime+ '&enterpriseCode=' + config.getUser().enterpriseCode;
            }
        });

        // 部门
        admin.req('user/admin/department/list', {limit:9999}, res => {
            if (res.code == 200){
                loadReportSelectHtml(res.data,'请选择部门','departmentId',1,'.lay-header')
            }else {
                loadReportSelectHtml([],'请选择部门','departmentId',1,'.lay-header')
            }
        });

        //部门操作
        form.on('select(departmentId)',function(data){
            selectManager(data.value);
        });

        // 操作人
        function selectManager(_data) {
            if (_data){
                admin.req('user/admin/user/list', {limit:9999, departmentId:_data}, res => {
                    if (res.code == 200){
                        loadReportSelectHtml(res.data,'请选操作人','creatorId',2,'.lay-header')
                    }else {
                        loadReportSelectHtml([],'请选操作人','creatorId',2,'.lay-header')
                    }
                });
            }else {
                loadReportSelectHtml([],'请选操作人','creatorId',2,'.lay-header')
            }
        }

        // 渲染下拉框
        function loadReportSelectHtml(data, tips, htmltarget, type, parentName) {
            let html = ' <option value="">' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].creatorName}</option>`;
                }
            }
            $(parentName).find("select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>